package com.tangmu.rabbits.ui.fragment.ticket;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.adapter.HomeActivitiesAdapter;
import com.tangmu.rabbits.base.BaseOneFragment;
import com.tangmu.rabbits.ui.activity.optimization.ScreenActivity;
import com.tangmu.rabbits.ui.fragment.BlankFragment;
import com.tangmu.rabbits.widget.ThumbnailView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 超级券
 */
public class TicketFragment extends BaseOneFragment implements ViewPager.OnPageChangeListener {

    public static final String TAG = "TicketFragment";
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    @BindView(R.id.iv_header_left)
    ThumbnailView ivHeaderLeft;
    @BindView(R.id.tv_header_title)
    TextView tvHeaderTitle;
    @BindView(R.id.iv_header_right)
    TextView ivHeaderRight;
    @BindView(R.id.ticket_headerView)
    LinearLayout ticketHeaderView;
    @BindView(R.id.optimization_top_recyclerView)
    RecyclerView topRecyclerView;
    @BindView(R.id.optimization_tabLayout)
    TabLayout mTabLayout;
    //
    @BindView(R.id.tv_goods_all)
    TextView tvGoodsAll;
    @BindView(R.id.tv_goods_sales_volume)
    TextView tvGoodsSalesVolume;
    @BindView(R.id.tv_goods_newest)
    TextView tvGoodsNewest;
    @BindView(R.id.tv_goods_price)
    TextView tvGoodsPrice;
    @BindView(R.id.iv_price_up)
    ImageView ivPriceUp;
    @BindView(R.id.iv_price_down)
    ImageView ivPriceDown;
    @BindView(R.id.layout_tabs)
    LinearLayout layoutTabs;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.vp_optimization)
    ViewPager mViewPager;

    private HomeActivitiesAdapter adapter;
    private List<Integer> tops = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabs = {"全部", "女装", "男装", "内衣", "美妆", "居家", "母婴", "食品", "鞋包"};


    public TicketFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TicketFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TicketFragment newInstance(String param1, String param2) {
        TicketFragment fragment = new TicketFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColor(R.color.transparent).titleBar(R.id.ticket_headerView).init();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_ticket;
    }

    @Override
    protected void initView() {
        tvHeaderTitle.setText(mActivity.getResources().getString(R.string.ticket));
        ivHeaderRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ScreenActivity.class);
            }
        });

        for (int i = 0; i < 4; i++) {
            tops.add(i);
        }
        topRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
        //topRecyclerView.setNestedScrollingEnabled(false);
        //topRecyclerView.setFocusableInTouchMode(false);
        //topRecyclerView.requestFocus();
        adapter = new HomeActivitiesAdapter(mActivity, tops);
        topRecyclerView.setAdapter(adapter);

        for (int i = 0; i < tabs.length; i++) {
            fragments.add(BlankFragment.newInstance(tabs[i]));
        }

        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.addOnPageChangeListener(this);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
